$(function(){
	
});


/**
 * 初始化分页信息
 * @returns
 */
function initPage(){
	//初始化
	initData();

	//上一页点击事件
	$("#pageInfo").on("click","#lastPage",function(){
		var currentPage=parseInt($("#currentPage").val());
		var totalPages=parseInt($("#totalPages").val());
		if(currentPage-1<0){
			return false;
		}
		goPage(currentPage-1,totalPages);
		return false;
	})
	
	//下一页点击事件
	$("#pageInfo").on("click","#nextPage",function(){
		var currentPage=parseInt($("#currentPage").val());
		var totalPages=parseInt($("#totalPages").val());
		if(currentPage+1>totalPages){
			currentPage=-1;
		}
		goPage(currentPage+1,totalPages);
		return false;
	})
	
	//首页
	$("#pageInfo").on("click","#homePage",function(){
		var currentPage=1;
		var totalPages=parseInt($("#totalPages").val());
		if(totalPages==0){
			return false;
		}
		goPage(currentPage,totalPages);
		return false;
	})
	
	//尾页
	$("#pageInfo").on("click","#endPage",function(){
		var totalPages=parseInt($("#totalPages").val());
		var currentPage=totalPages;
		goPage(currentPage,totalPages);
		return false;
	})
	
	//GO 跳转
	$(".pagelist").on("click","#goToBtn",function(){
		var totalPages=parseInt($("#totalPages").val());
		var currentPage=parseInt($("#goTo").val()==""?"1":$("#goTo").val());//获取跳转的页码数
		if(currentPage>totalPages){
			return false;
		}
		goPage(currentPage,totalPages);
		return false;
	})
	
	//查询功能
	$(".search_btn").click(function(){
		var key=$("#searchTitle").val();
		$("#title").val(key);//将值放入隐藏域中
		$("#currentPage").val(1);//将页码值为1
		$("#type").val("");//将项目详情值为空
		//initData();//查询请求
		//页面跳转
		window.location.href="../web/searchList?title="+key;
	});
}


   
/**
*生成分页码 
*/
function goPage(currentPage,totalPages){
	var script='';
	//首页
	script+='<li><a href="#" id="homePage" >首页</a></li>';
	var count=5;//显示几个
	var every=parseInt(count/2);

	//上一页
	if(currentPage==1||currentPage==0){//第一页
		script+='<li><a href="#" onclick="return false" class="undown">上页</a></li>';
	}else{
		script+='<li><a href="#" id="lastPage">上页</a></li>';
	}
	

	//拼前面的
	for (var i = 1; i <currentPage; i++) {
		if(currentPage+every>=totalPages){//
			if (currentPage-(count-(totalPages-currentPage))<i) {
				script+='<li><a href="javascript:void(0)" onclick="goPage('+i+','+totalPages+');">'+i+'</a></li>';
			}
		}else{
			if (currentPage-every<=i) {
				script+='<li><a href="javascript:void(0)" onclick="goPage('+i+','+totalPages+');">'+i+'</a></li>';
			}
		}
	}
	//拼当前的
	script+='<li class="current">'+currentPage+'</li>';

	//拼后面的
	for (var i = currentPage+1; i <= totalPages; i++) {
		if (currentPage-every<=0) {//
			if (currentPage+count-currentPage>=i) {
				script+='<li><a href="javascript:void(0)" onclick="goPage('+i+','+totalPages+');">'+i+'</a></li>';
			}
		}else{
			if (currentPage+every>=i) {
				script+='<li><a href="javascript:void(0)" onclick="goPage('+i+','+totalPages+');">'+i+'</a></li>';
			}
		}
		
	}

	//下一页
	if(currentPage==totalPages||currentPage==0||(currentPage==1&&totalPages==0)){//最后一页
		script+='<li><a href="#" class="undown" onclick="return false">下页</a></li>';
	}else{
		script+='<li><a href="#" id="nextPage">下页</a></li>';
	}
	
	//尾页
	script+='<li><a href="#" id="endPage">尾页</a></li>';
	
	$("#pageInfo").html(script);
	
	//将分页参数放入隐藏域中
	$("#currentPage").val(currentPage);
	
	//修改Go的页码
	$("#goTo").val(currentPage);
	
	//分页 请求 ajax
	pageForm();
	
	
	return script;
}

/**
 * 分页 请求 ajax
 * @returns
 */
function pageForm(){
	$.ajax({
		type: "post",
		url:  "../web/contentLists",
		data:$("#pageForm").serialize(),
		success: function(data,res){
			var html='';
			var json=JSON.stringify(data.rows);
			var result=eval("("+json+")");
			for (var i = 0; i < data.rows.length; i++) {
				var endShowDate=new Date(Date.parse(data.rows[i].endShowTime.replace(/-/g, "/")));//展示结束（开标时间）
				var date=new Date(Date.parse(data.rows[i].createTime.replace(/-/g, "/")));//创建时间
				var top=data.rows[i].title;
				var title=data.rows[i].title;
				if(title.length>25){
					title=title.substring(0,25)+'......';
				}
				//alert(data.rows[i].isOpen);
				if(data.rows[i].isOpen==0){//未开标
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="no_open">•【未开拍】</span>'+
					title+'</span><span class="right_time">'+endShowDate.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else if(data.rows[i].isOpen==1){
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="open">•【已结束】</span>'+
					title+'</span><span class="right_time">'+endShowDate.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else if(data.rows[i].isOpen==2){
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="no_open">•【正在公示】</span>'+
					title+'</span><span class="right_time">'+date.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else if(data.rows[i].isOpen==3){
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="open">•【公示结束】</span>'+
					title+'</span><span class="right_time">'+date.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else {
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="open">• </span>'+
					title+'</span><span class="right_time">'+date.Format("yyyy年MM月dd日")+'</span></a></li>'
				}
			}
			$("#contentList").html(html);
			
			//将分页参数放入隐藏域中
			$("#totalPages").val(data.totalPages);
			$("#totalTxt").text(data.totalPages);//页面显示总页数
			
		},
		error: function(res){
			//alert(3);
		}
	});
}


/**
 * 初始化数据
 * @returns
 */
function initData(){
	clearPage();//初始化分页参数
	$.ajax({
		type: "post",
		url:  "../web/contentLists",
		data:$("#pageForm").serialize(),
		success: function(data,res){
			var html='';
			var json=JSON.stringify(data.rows);
			var result=eval("("+json+")");
			for (var i = 0; i < data.rows.length; i++) {
				var endShowDate=new Date(Date.parse(data.rows[i].endShowTime.replace(/-/g, "/")));//展示结束（开标时间）
				var date=new Date(Date.parse(data.rows[i].createTime.replace(/-/g, "/")));//创建时间
				var top=data.rows[i].title;
				var title=data.rows[i].title;
				if(title.length>25){
					title=title.substring(0,25)+'......';
				}
				//alert(data.rows[i].isOpen);
				if(data.rows[i].isOpen==0){//未开标
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="no_open">•【未开标】</span>'+
					title+'</span><span class="right_time">'+endShowDate.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else if(data.rows[i].isOpen==1){
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="open">•【已开标】</span>'+
					title+'</span><span class="right_time">'+endShowDate.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else if(data.rows[i].isOpen==2){
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="no_open">•【正在公示】</span>'+
					title+'</span><span class="right_time">'+date.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else if(data.rows[i].isOpen==3){
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="open">•【公示结束】</span>'+
					title+'</span><span class="right_time">'+date.Format("yyyy年MM月dd日")+'</span></a></li>'
				}else {
					html+='<li title="'+top+'"><a href="getWebDetail?id='+data.rows[i].id+'" target="_blank"><span class="left_title"><span class="open">• </span>'+
					title+'</span><span class="right_time">'+date.Format("yyyy年MM月dd日")+'</span></a></li>'
				}
			}
			$("#contentList").html(html);
			
			//将分页参数放入隐藏域中
			$("#totalPages").val(data.totalPages);
			$("#totalTxt").text(data.totalPages);//页面显示总页数
			
			var currentPage=parseInt($("#currentPage").val());
			var totalPages=parseInt($("#totalPages").val());
			
			goPage(currentPage,totalPages);//生成分页码
			
		},
		error: function(res){
			//alert(3);
		}
	});
}

/**
 * 初始化分页参数
 * @returns
 */
function clearPage(){
	$("#currentPage").val(1)
}

/******************************公共工具方法*************************************/

/**
 * 时间格式化方法
 */
Date.prototype.Format = function (fmt) { //
    var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

